package com.hk.admin.common.model.system;

import com.hk.admin.common.constant.GlobalConstants;
import com.hk.admin.common.constant.GlobalEnum;
import com.hk.admin.common.constant.system.RoleEnum;
import com.hk.admin.common.dto.system.role.RoleDTO;
import com.hk.admin.common.dto.system.role.RoleSearchDTO;
import lombok.Data;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.*;
import javax.persistence.criteria.Predicate;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Data
@Table(name = GlobalConstants.TABLE_PREFIX + "role")
public class RoleModel implements Serializable {

    @Id
    @GeneratedValue
    private Long id;                 // id主键
    private String roleName;        // 角色名称
    private Integer roleStatus;     // 角色状态, 0:启用 ，1:禁用
    private String description;     // 角色描述

    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;        //  创建时间
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;        //  修改时间

    private Integer deleted;        // 删除状态

    @JoinTable(name = GlobalConstants.TABLE_PREFIX + "role_menu", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    private List<MenuModel> menus;

    @ManyToMany(mappedBy = "roles")
    private List<UserModel> users;

    public RoleModel() {
    }

    public RoleModel(RoleDTO dto) {
        this.id = dto.getId();
        this.roleName = dto.getRoleName();
        this.description = dto.getDescription();
        this.roleStatus = RoleEnum.ROLE_STATUS.ENABLE.getKey();  // 默认启用
        this.deleted = GlobalEnum.DELETED.NO.getKey(); // 默认未删除
        this.createTime = new Date();
    }

    /**
     * 更新
     */
    public void update(RoleDTO dto) {
        this.roleName = dto.getRoleName();
        this.description = dto.getDescription();
        this.updateTime = new Date();
    }

    /**
     * 启用
     */
    public void enable() {
        this.roleStatus = RoleEnum.ROLE_STATUS.ENABLE.getKey();
        this.updateTime = new Date();
    }

    /**
     * 禁用
     */
    public void disable() {
        this.roleStatus = RoleEnum.ROLE_STATUS.DISABLE.getKey();
        this.updateTime = new Date();
    }

    /**
     * 删除
     */
    public void delete() {
        this.deleted = GlobalEnum.DELETED.YES.getKey();
        this.updateTime = new Date();
    }

    public static Specification<RoleModel> createSpecification(RoleSearchDTO dto) {
        return (root, cq, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (dto.getRoleName() != null && !"".equals(dto.getRoleName())) {
                predicates.add(cb.like(root.get("roleName"), "%" + dto.getRoleName()));
            }
            predicates.add(cb.equal(root.get("deleted"), GlobalEnum.DELETED.NO.getKey()));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }

    /**
     * 判断是否删除
     */
    public boolean alreadyDeleted() {
        return this.deleted != null && this.deleted.equals(GlobalEnum.DELETED.YES.getKey());
    }

}
